<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */
namespace Common\Api;
use Common\Api\Api;
use Admin\Model\GamePrizegethisModel;
use Admin\Model\MessageModel;
use Admin\Model\MessageBoxModel;
class GameLogApi extends Api{
  const ADD     = "Common/GameLog/addLog";
  const PAGE    = "Common/GameLog/page";
  const HISPAGE = "Common/GameLog/getHisByGid";
  const QUERY   = "Common/GameLog/queryNoPaging";
  const FINDALL = "Common/GameLog/findAll";
	//初始化
  protected function _init(){
  	// parent::
    $this -> model  = new GamePrizegethisModel();
    $this -> msg    = new MessageModel();
    $this -> msgBox = new MessageBoxModel();
  }

  public function findAll($gid){
    $r = $this->model->alias('h') //->cache(true)
      ->join(C('DB_PREFIX').'game_prize p ON h.prize_id = p.id')
      ->join('common_member m ON h.uid = m.uid')
      ->where(array('h.game_id'=>$gid))
      ->field('h.uid id,p.prize_name name,m.nickname')->select();
      // dump($r);exit;
    return $this->apiReturnSuc($r);
  }
  public function page($uid,$gid,$start=0,$size=10){
    $r = $this->model->alias('h') //->cache(true)
      ->join(C('DB_PREFIX').'game_prize p ON h.prize_id = p.id')
      ->join('common_member m ON h.uid = m.uid')
      ->where(array('h.game_id'=>$gid,'h.uid'=>$uid))
      ->field('h.uid id,p.prize_name name,m.nickname')
      ->limit($start,$size)
      ->select();
    return $this->apiReturnSuc($r);
  }

  public function addLog($map){
  	$m = array(
      'dtree_type'   => 6055
      ,'content'     => $map['content']
      ,'send_time'   => NOW_TIME
      ,'create_time' => NOW_TIME
      ,'from_id'     => 0
      ,'title'       => '[中奖信息]'
      ,'summary'     => $map['summary']
  		,'status'      => 1
  	);
    // dump($m);exit;
  	if($this->msg->create($m,1)){
  		if($this->msg->data($m)->add()){
	  		$mid = $this->msg->getLastInsID();
	  		$m = array(
	  			'msg_id' => $mid
	  			,'to_id' => $map['uid']
	  			,'msg_status'=> 0
	  		);
	  		if($this->msgBox->create($m,1)){
	  			if($this->msgBox->data($m)->add()) return $this->apiReturnSuc('抽奖已记录');
	  			else return $this->apiReturnErr($this->msgBox->getDbError());
	  		}else{
	  			return $this->msgBox->apiReturnErr($this->msgBox->getError());
	  		}
  		}else{
  			return $this->apiReturnErr($this->msg->getDbError());
  		}
  	}else{
  		return $this->apiReturnErr($this->msg->getError());
  	}
  }

  public function getHisByGid($gid,$page){
  	$where = array('game_id'=>$gid);
    $page = array('curpage'=>$page,'size'=>10);

  	$r = $this->model->where($where); //->cache(true)
    $list = $r -> page($page['curpage'].','.$page['size']) -> select();
		if ($list === false) {
			$error = $this -> model -> getDbError();
			return $this -> apiReturnErr($error);
		}
		$count = $r -> count();
		$Page = new \Think\Page($count, $page['size']);
		//分页跳转的时候保证查询条件
		if ($params !== false) {
			foreach ($params as $key => $val) {
				$Page -> parameter[$key] = urlencode($val);
			}
		}
		$show = $Page -> show();
		return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
  }
}